/**
 * 礼品码兑换和展示 20171206  mxm
 */
define(function(require,exports,module) {
    var myPub = require('./public');

    var oWindow = $(window);
    var oHeader = $('.js_headerBox');               // 头部
    var oFooter = $('.js_footer');                  // 底部
    var oContain = $('.js_contain');                // 页面内容区域

    var oGiftMaTxt = $('.js_giftMaTxt');
    var oGiftMa_TipTxt = $('.js_giftMa_tipTxt');
    var oSubmitGiftMa = $('.js_submitGiftMa');

    var  oClearBtn = $('.js_clearBtn');

    var offTap = true;                  // 阻止多次点击
    var moreTapOffTime = null;          // 多次点击定时器
    var Testing = false;                // 校验按钮是否可点击
    var maxLength = 10;                 // 校验输入框的最长内容

    var errorTip = ['在使用礼品码前，请先输入礼品码编号验证', '礼品码编号错误，请重新输入'];
    var layerTip = ['在使用礼品码之前需要绑定您的手机号', '恭喜您，验证通过!'];
    var layerStyle_black = 'background-color:rgba(0,0,0, 0.6); color:#FFFFFF; border:none; border-radius: 5px; min-width: 50%;';


    var resizeTimer = null;
    // 页面尺寸修改
    oWindow.on('resize',function() {
        if (resizeTimer) {
            clearTimeout(resizeTimer)
        }
        resizeTimer = setTimeout(function(){
            oContain.css({
                'padding-top': oHeader.outerHeight(true),
                'padding-bottom' : oFooter.outerHeight(true)
            });
        }, 400);

    });

    // 表单验证
    oGiftMaTxt.on('input propertychange',function() {
        // 通过前端验证 修改提示和改变检测状态
        ValiNumLetter(oGiftMaTxt, function () {
            Testing = false;
            oGiftMa_TipTxt.text(errorTip[1]);
        }, function () {
            Testing = true;
            oGiftMa_TipTxt.text(errorTip[0]);
        });

        // 满足指定数字个数开始验证
        if(oGiftMaTxt.val().length === maxLength) {
           Testing ? oSubmitGiftMa.addClass('active') : oSubmitGiftMa.removeClass('active');
        } else {
            oSubmitGiftMa.removeClass('active');
        }
    });

    // 提交按钮操作
    oSubmitGiftMa.hammer().on('tap', function () {
        // 变亮状态 还有active
        if($(this).hasClass('active')) {
            // 防止多次点击
            if(offTap) {
                oGiftMaTxt.blur();
                offTap = false;
                if(!isBind){
                    // 绑定手机弹框
                    myPub.askLayer(layerTip[0], function () {
                        // 立即绑定
                        myPub.LayerCloseAll();
                        var backHref = "/giftCode/giftCodeList";
                        window.location.href = "/h/login/bindMobile?backHref=" + backHref;
                    }, function () {
                        // 取消
                        myPub.LayerCloseAll();
                    }, 'no', null, ['立即绑定','取消']);
                }

                checkAndDrawGiftCode();
            }
            clearTimeout(moreTapOffTime);
            moreTapOffTime = setTimeout(function () {
                offTap = true;
            }, 500);
        }
    });
    
    function checkAndDrawGiftCode() {
        var codeSign = oGiftMaTxt.val();
        G_JSON_POST('/giftCode/drawGiftCode',{giftCodeSign:codeSign},function (data) {
            if (data.status == "success") {
                myPub.TipLayer("验证成功",null,function () {
                    window.location.href = "/giftCode/giftCodeList";
                });
            } else {
                oGiftMa_TipTxt.text(data.message);
            }
        });
    }

    // 清空按钮操作
    oClearBtn.hammer().on('tap', function () {
        oGiftMaTxt.val('');
        oGiftMa_TipTxt.text(errorTip[0]);
        oSubmitGiftMa.removeClass('active');
    })

    showDrawMsg();
    function showDrawMsg() {
        if(typeof(drawMsg) == 'undefined'){
            return;
        }
        if(drawMsg != null && drawMsg != ""){
            myPub.TipLayerEnd(drawMsg,null);
        }
    }

});


/* 数字字母验证 */
function ValiNumLetter(obj,fn1,fn2) {
    var reg = new RegExp("^[a-zA-Z0-9]*$");
    if(!reg.test(obj.val())){
        if(fn1) fn1();
    }
    else{if(fn2) fn2();}
}